
/**
 * historyData vuex
 */
import posts from '@/plugins/request'
import common from '@/plugins/common'
import { realform, reallist } from "@/http/api.js";
// initial state
const state = {
  historyDataList: {
    data: [],
    total: 0,
    factorField: [],
    factorStatus: {},
  },
  curDetail: {},
  repair: {
    station: '',
    date: '',
    remark: '',
    person: '',
    content: {
      failureReason: '',
      solutions: '',
      deviceName: '',
      modelType: '',
      replaceCnt: '',
      replaceNum: '',
      calibration: [],
    }
  },
  maintenance: {
    station: '',
    date: '',
    remark: '',
    person: '',
    content: {
      deviceName: '',
      modelType: '',
      maintenType: [],
      OverhaulRes: '',
      thisTime: '',
      LastTime: '',
    }
  },
  inspect: {
    station: '',
    date: '',
    remark: '',
    person: '',
    content: {
      stationAmbient: [],
      stationAmbientRes: '',
      stationAmbientCnt: '',
      appSystem: [],
      appSystemRunState: '',
      appSystemPower: '',
      appSystemTemper: '',
      appSystemTemperSensor: '',
      appSystemStirMotor: '',
      appSystemAirPump: '',
      appSystemWaterExtraction: '',
      appSystemIPC: '',
      appSystemCnt: '',
      vulnerableParts: [
        {
          vulnerablePartsRes: '',
          vulnerablePartsCnt: '',
        },
      ],
      consumables: [],
      consumablesRes: '',
      consumablesGroup: [
        {
          consumablesName: '',
          consumablesNum: '',
        },
      ],
      reagent: [],
      reagentRes: '',
      reagentGroup: [
        {
          reagentName: '',
          reagentNum: '',
        },
      ],
      piping: [],
      pipingRes: '',
      pipingGroup: [
        {
          pipingName: '',
          pipingNum: '',
        },
      ],
    },
  },
}
// getters
const getters = {
  getHistoryData: state => state.historyDataList.data,
  getHistoryDataTotal: state => state.historyDataList.total,
  getHistoryDataFactorField: state => state.historyDataList.factorField,
  getHistoryDataFactorStatus: state => state.historyDataList.factorStatus,
  getCurDetail: state => state.curDetail
}
// actions
const actions = {
  async getHistoryDataChart({ commit }, params) {
    let data;
    let paramsdata = {
      isExcel: params.isExcel,
      lasttime: params.lasttime,
      starttime: params.starttime,
      waterstation: params.waterstation
    }
    if (params.dataType === 'hour' || params.dataType === 'oneHour') {
      data = await posts.fetchHistoryDataChart(paramsdata)
      return data;
    } else {
      data = await realform(paramsdata)
      data.data.factorMessage = [
        { name: "pH", factorLimit: 0.1, standardValue: "6.5~9.5" },
        { name: "ORP", standardValue: null, factorLimit: 0.1 },
        { name: "色度", standardValue: null, factorLimit: 0.1 },
        { name: "TOC", standardValue: null, factorLimit: 0.1 },
        { name: "COD", standardValue: null, factorLimit: 0.1 },
        { name: "浊度", standardValue: null, factorLimit: 0.1 },
        { name: "UV254", standardValue: null, factorLimit: 0.1 },
      ]
      return data.data
    }
  },
  async getHistoryDataList({ commit }, params) {
    let data;
    let paramsdata = {
      isExcel: params.isExcel,
      lasttime: params.lasttime,
      starttime: params.starttime,
      waterstation: params.waterstation,
      pageNum: params.pageNum,
      pageSize: params.pageSize
    }
    if (params.dataType === 'hour' || params.dataType === 'oneHour') {
      data = await posts.fetchHistoryDataList(params)
    } else {
      data = await reallist(paramsdata)
      data.data.factorMessage = [
        { name: "pH", factorLimit: 0.1, standardValue: "6.5~9.5" },
        { name: "ORP", standardValue: null, factorLimit: 0.1 },
        { name: "色度", standardValue: null, factorLimit: 0.1 },
        { name: "TOC", standardValue: null, factorLimit: 0.1 },
        { name: "COD", standardValue: null, factorLimit: 0.1 },
        { name: "浊度", standardValue: null, factorLimit: 0.1 },
        { name: "UV254", standardValue: null, factorLimit: 0.1 },
      ]
    }
    commit('changeHisDataList', data.data)
  },
  async downHistoryDataListisExcel({ commit }, param) {
    await posts.postHistoryDataListExcel(param)
  },
  async fetchRetrieveData({ commit }, param) {
    const res = await posts.postRetrieveData(param)
    return res
  },
}

// mutations
const mutations = {
  changeHisDataList(state, data) {
    let unit = '';
    state.historyDataList.data = data ? data.data : []
    state.historyDataList.total = data ? data.totalpage : 0
    state.historyDataList.factorStatus = data ? data.factorStatus : {}
    state.historyDataList.factorField = []
    data.factorMessage.forEach((item) => {
      if (Object.keys(data.factorField).includes(item.name)) {
        if (item.name === "pH") {
          unit = '';
        } else if (item.name === "ORP") {
          unit = '(mV)';
        } else if (item.name === "ORP") {
          unit = '';
        } else if (item.name === "UV254") {
          unit = '(AU/cm)';
        } else if (item.name === "浊度") {
          unit = '(NTU)';
        } else {
          unit = 'mg/L';
        }
        state.historyDataList.factorField.push({
          prop: data.factorField[item.name],
          label: item.name + unit,
          devStatus: item.name,
          standardValue: item.standardValue,
          factorLimit:
            item.name === "pH"
              ? "±" + item.factorLimit + "L"
              : item.factorLimit + "L",
        });
      }
    })
  },
  resetData(state) {
    state.historyDataList = {
      data: [],
      total: 0,
      factorField: [],
      factorStatus: {},
    }
  },
  changeCurDetail(state, data) {

    // let obj = common.deepClone(state[data.type])

    // obj.content = 

    // debugger
    // state[data.type] = {...state[data.type], ...data}

    // state[data.type].content = {...state[data.type].content, ...data.content}

    state.curDetail = data


  },
}
export default {
  state,
  getters,
  actions,
  mutations
}
